package play.cnblog.cli.config;

import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import play.cnblog.cli.rpc.CnblogRpc;
import play.cnblog.cli.rpc.impl.CnblogXmlRpc;
import play.cnblog.cli.service.BlogService;
import play.cnblog.cli.service.CnblogMediaService;
import play.cnblog.cli.service.CnblogService;
import play.cnblog.cli.service.impl.BlogServiceImpl;
import play.cnblog.cli.service.impl.CnblogMediaServiceImpl;
import play.cnblog.cli.service.impl.CnblogServiceImpl;

/**
 * @author LiJie
 */
public class GlobalConfig {

    private static CnblogService cnblogService;

    public static CnblogService cli() {
        return cnblogService;
    }

    public static void initModel(Properties props) {
        String username = props.getProperty("username");
        String password = props.getProperty("password");
        String mateWeblogUrl = props.getProperty("metaWeblog.url");
        String localProtocol = props.getProperty("local.protocol");
        String localMediaDir = props.getProperty("local.mediaDir");
        if (StringUtils.isBlank(username)
            || StringUtils.isBlank(password)
            || StringUtils.isBlank(mateWeblogUrl)) {

            throw new IllegalArgumentException
                ("username, password, metaWeblog.url not exist");
        }

        CnblogRpc cnblogRpc =
            new CnblogXmlRpc(mateWeblogUrl, username, password);
        BlogService blogService = new BlogServiceImpl();
        CnblogMediaService cnblogMediaService = new CnblogMediaServiceImpl(
            cnblogRpc, localProtocol, localMediaDir);
        cnblogService = new CnblogServiceImpl(cnblogRpc, blogService,
            cnblogMediaService);
    }
}
